// pages/organization/admin/modify/org_name.js
var app = getApp();
var util = require("../../../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // item: "",
    // item_type: "",
    // item_name: "",
    // item_default:"",
    // org_id: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    console.log(options);
    wx.setNavigationBarTitle({
      title: options.item_name,
    })
    this.setData({
      item: options.item,
      item_type: options.item_type,
      item_name: options.item_name,
      item_default: options.item_default,
      org_id: options.org_id,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 服务器

  fixed_org_password_request(e) {
    var that = this;
    var password = e.detail.value.org_password;
    var yuyue_session = wx.getStorageSync("yuyue_session");
    var org_id = this.data.org_id;
    console.log(password);
    wx.request({
      url: app.globalData.default_url,
      data: {
        action: "api.v1.organization.setPassword",
        org_id: org_id,
        yuyue_session: yuyue_session,
        password: password
      },
      success: function (res) {
        console.log(res);
        wx.setStorageSync("org", res.data.data);
        if (res.data.data.op != "fail")
          wx.showModal({
            title: '修改成功',
            content: '成功修改口令',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                wx.navigateBack({

                })
              }
            }
          })
        else{
          wx.showModal({
            title: '修改失败',
            content: res.data.data.reason,
            confirmText: "重试",
            success: function (res) {
              if (res.confirm) {
                that.fixed_org_password_request(e);
                return;
              }

            }

          })
        }
      }, fail: function (res) {
        console.log(res);
        wx.showModal({
          title: '网络错误',
          content: "请检查网络连接",
          confirmText: "重试",
          success: function (res) {
            if (res.confirm) {
              that.fixed_org_password_request(e);
              return;
            }

          }

        })
      }
    })
  },
  fixed_org_name_request(e) {
    var that = this;
    var org_name = e.detail.value.org_name;
    var yuyue_session = wx.getStorageSync("yuyue_session");
    var org_id = this.data.org_id;
    console.log(org_name);
    wx.request({
      url: app.globalData.default_url,
      data: {
        action: "api.v1.organization.setName",
        org_id: org_id,
        yuyue_session: yuyue_session,
        org_name: org_name
      },
      success: function (res) {
        console.log(res);
        wx.setStorageSync("org", res.data.data);
        if (res.data.data.op != "fail")
          wx.showModal({
            title: '修改成功',
            content: '成功修改组织名称',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                wx.navigateBack({

                })
              }
            }
          })
        else {
          wx.showModal({
            title: '修改失败',
            content: res.data.data.reason,
            confirmText: "重试",
            success: function (res) {
              if (res.confirm) {
                that.fixed_org_name_request(e);
                return;
              }

            }

          })
        }
      }, fail: function (res) {
        console.log(res);
        wx.showModal({
          title: '网络错误',
          content: "请检查网络连接",
          confirmText: "重试",
          success: function (res) {
            if (res.confirm) {
              that.fixed_org_name_request(e);
              return;
            }

          }

        })
      }
    })
  }
})